package jetpack.com.pzl.architecture.data_binding;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.databinding.DataBindingUtil;

import java.util.List;

import jetpack.com.pzl.R;
import jetpack.com.pzl.databinding.ItemListBinding;

//如果您要在 Fragment、ListView 或 RecyclerView 适配器中使用数据绑定项，您可能更愿意使用绑定类或 DataBindingUtil 类的 inflate() 方法
// ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
// or
//  ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
public class ListAdapter extends BaseAdapter {

    private Context context;

    private List<ListItemBean> data;

    public List<ListItemBean> getData() {
        return data;
    }

    public ListAdapter(List<ListItemBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemListBinding binding;
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_list, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (ItemListBinding) convertView.getTag();
        }

        //BR.itemBean 系統生成。id: jetpack.com.pzl.BR.itemBean 布局data标签里定义的数据类别名的id。data.get(position) ：需要填充的数据。
        binding.setVariable(jetpack.com.pzl.BR.itemBean, data.get(position));
        return binding.getRoot();
    }


}
